//
//  HYLImageHandleView.m
//  画板实现
//
//  Created by hyl on 16/6/30.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLImageHandleView.h"

@interface HYLImageHandleView ()<UIGestureRecognizerDelegate>

@end

@implementation HYLImageHandleView


#pragma mark - setter/getter
-(UIImageView *) imageView{
    if (_imageView==nil) {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds];
        _imageView=imageView;
        //注意点，允许用户交互，不能响应事件时一般查这个
        _imageView.userInteractionEnabled=YES;
        self.backgroundColor=[UIColor clearColor];
        [self setPan];
        [self setLongPress];
        [self setRotation];
        [self setPinch];
        [self addSubview:_imageView];
    }
    return _imageView;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
//    NSLog(@"%@    ===",NSStringFromCGPoint(point));
    CGPoint childP=[self convertPoint:point fromCoordinateSpace:self.imageView];
//    NSLog(@"%@    --",NSStringFromCGPoint(point));
//    if ([self.imageView pointInside:childP withEvent:event]) {
//        return self.imageView;
//    }else{
//        return nil;
//    }
    UIView *view=[self.imageView hitTest:childP withEvent:event];
    return view;
}
#pragma mark - setpan
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.imageView addGestureRecognizer:pan];
}
-(void) pan:(UIPanGestureRecognizer *)pan{
    CGPoint transP=[pan translationInView:self.imageView];
    self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
    //复位
    [pan setTranslation:CGPointZero inView:self.imageView];
}
#pragma mark -setLongPress
-(void) setLongPress{
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    [self.imageView addGestureRecognizer:longPress];
}
-(void) longPress:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state==UIGestureRecognizerStateBegan) {
        //做一个高亮的动画
        
        
        
        [UIView animateWithDuration:0.25 animations:^{
            self.imageView.alpha=0;
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.25 animations:^{
                self.imageView.alpha=1;
            } completion:^(BOOL finished) {
                //开启图片上下文
                UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
                //获得图片上下文
                CGContextRef context=UIGraphicsGetCurrentContext();
                //将layer加到图片上下文上,这个图片在整个imageView上，所以重绘时是不对的
//                [self.imageView.layer renderInContext:context];
                [self.layer renderInContext:context];
                
                //从图片上下文上获得图片
                UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
//                NSLog(@"%@",image);
                //关闭图片上下文
                UIGraphicsEndImageContext();
                
//                self.imageView.image=image;
                //重画drawView，执行block
                if (self.imageHandleCompletionBlock) {
                    self.imageHandleCompletionBlock(image);
                }
                
                //移除self.imageView
                [self.imageView removeFromSuperview];
                [self removeFromSuperview];
            }];
            
            
            
        }];
    }
}

#pragma mark - setRotation
-(void) setRotation{
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [self.imageView addGestureRecognizer:rotation];
}

-(void) rotation:(UIRotationGestureRecognizer *)rotation{
    CGFloat r=rotation.rotation;
    self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, r);
    rotation.rotation=0;
    
}
#pragma mark -setPinch
-(void) setPinch{
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    pinch.delegate=self;
    [self.imageView addGestureRecognizer:pinch];
}
-(void) pinch:(UIPinchGestureRecognizer *)pinch{
//    NSLog(@"%s",__func__);
    self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    //复位
    pinch.scale=1;
    
}

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}




@end
